package com.wing.device.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.enums.InspectEnum;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.device.model.entity.DeviceInspect;
import com.wing.device.model.entity.DeviceInspectClaim;
import com.wing.device.model.entity.DeviceInspectExecute;
import com.wing.device.model.request.DeviceInspectClaimForm;
import com.wing.device.model.response.DeviceInspectClaimVO;
import com.wing.device.service.DeviceInspectClaimService;
import com.wing.device.service.DeviceInspectExecuteService;
import com.wing.device.service.DeviceInspectService;
import com.wing.web.db.config.util.DynamicDataSource;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.1.0
 * @Author hz
 * @Date 2024-08-19 15:13
 **/
@Validated
@Api(value = "Admin-设备维修任务管理", tags = "#/device/inspectClaim")
@RestController("inspectClaimController")
@RequestMapping("device/admin/inspectClaim")
@Slf4j
public class DeviceInspectClaimController {


    @Resource
    private DeviceInspectClaimService deviceInspectClaimService;

    @Resource
    private DeviceInspectService deviceInspectService;

    @Resource
    private DeviceInspectExecuteService deviceInspectExecuteService;



    @ApiOperation(value = "查询设备年检任务列表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<DeviceInspectClaimVO> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,
            String deviceType,
            String inspectClaimStatus,
            String id,
            String deviceId,
            String taskNo
            ){
        Page<DeviceInspectClaim> pg = new Page<>(page, limit);
        Map map = new HashMap();
        map.put("deviceType", deviceType);
        map.put("inspectClaimStatus", inspectClaimStatus);
        map.put("id", id);
        map.put("deviceId", deviceId);
        map.put("taskNo", taskNo);
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        map.put("databaseUser", databaseUser);
        IPage<DeviceInspectClaimVO> list = deviceInspectClaimService.pageList4app(pg, map);
        //关联相关业务属性
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加年检任务", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime"})
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public JsonResult add(@RequestBody DeviceInspectClaimForm deviceInspectClaimForm) {
        try {
            if (null == deviceInspectClaimForm.getModifier()){
                deviceInspectClaimForm.setModifier(JwtUtils.getLoginUserId());
                deviceInspectClaimForm.setModifierName(JwtUtils.getLoginUsername());
                deviceInspectClaimForm.setUpdateTime(LocalDateTime.now());
            }
            if (null == deviceInspectClaimForm.getCreator()){
                deviceInspectClaimForm.setCreator(JwtUtils.getLoginUserId());
                deviceInspectClaimForm.setCreatorName(JwtUtils.getLoginUsername());
                deviceInspectClaimForm.setCreateTime(LocalDateTime.now());
            }
            // 获取任务编码
            DeviceInspect deviceInspect = deviceInspectService.getById(deviceInspectClaimForm.getInspectId());
            if(ObjectUtil.isEmpty(deviceInspect)){
                return JsonResult.fail("未找到年检任务编号！");
            }
            deviceInspect.setExamineType(deviceInspectClaimForm.getExamineType());
            deviceInspect.setInspectStatus(InspectEnum.InspectStatusEnum.WC.getCode());
            deviceInspect.setDoneTime(LocalDateTime.now());
            //赋值
            DeviceInspectExecute deviceInspectExecute = new DeviceInspectExecute();
            CopyOptions copyOptions = CopyOptions.create()
                    .setEditable(DeviceInspectExecute.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(deviceInspectClaimForm, deviceInspectExecute, copyOptions);
            deviceInspectService.updateById(deviceInspect);
            deviceInspectExecuteService.save(deviceInspectExecute);
            return JsonResult.success("添加成功");
        } catch (Exception e) {
            log.error("添加失败:", e);
            return JsonResult.fail(e.getMessage());
        }
    }


}
